<template>
  <div id="app">
    <a-input :value="inputValue" @change="iputChnange" placeholder="请输入任务" class="my_ipt" />
    <a-button @click="clickAddItem" type="primary">添加事项</a-button>

    <a-list bordered :dataSource="filterList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox @change="changeCheckbox(item.id)" :checked="item.done">{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" slot-scope="scope" @click="clickRemove(scope.row)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <!-- <span>{{$store.getters.unDone}}条剩余</span> -->
        <span>未完成{{unDone}}</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button @click="clickActive(0)" :type="activeIndex === 0 ? 'primary' : 'default'">全部</a-button>
          <a-button @click="clickActive(1)" :type="activeIndex === 1 ? 'primary' : 'default'">未完成</a-button>
          <a-button @click="clickActive(2)" :type="activeIndex === 2 ? 'primary' : 'default'">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clear">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapMutations, mapGetters } from 'vuex'
export default {
  name: 'app',
  data() {
    return {
    }
  },
  computed: {
    ...mapState(['list', 'inputValue', 'activeIndex']),
    ...mapGetters(['unDone', 'filterList']),
  },
  created () {
    this.$store.dispatch('getList')
  },
  methods: {
    iputChnange (e) {
      console.log('输入框变化了', e.target.value)
      this.$store.commit('viewInputValue', e.target.value)
    },
    clickAddItem () {
      if (this.inputValue.trim() === '') return this.$message.warning('请输入合法的任务')
      this.$store.commit('addItem')
    },
    clickRemove (id) {
      this.$store.commit('removeItem', id)
    },
    changeCheckbox (id) {
      this.$store.commit('changeDone', id)
    },
    clear () {
      this.$store.commit('clearDone')
    },
    clickActive (index) {
      this.$store.commit('setActiveIndex', index)
    }
  }
}
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>